<template>
  <div v-loading="loading">
    <el-upload
      action="#"
      :class="{ disabled: uploadDisabled }"
      list-type="picture-card"
      :file-list="uploadFileList"
      :http-request="httpRequest"
      :before-upload="beforeUpload"
      :accept="accept"
      :disabled="disabled"
    >
      <i slot="trigger" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{ file }">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script>
import publicApi from '@/api/public';
export default {
  props: {
    value: {
      default: () => ({}),
    },
    businessTable: {
      type: String,
      default: null,
    },
    businessId: {
      type: String,
      default: null,
    },
    subType: {
      type: String,
      default: null,
    },
    accept: {
      default: '.jpg',
    },
    maxSize: {},
    minSize: {},
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    value: {
      handler(data) {
        if (data.id) {
          let fileObj = {
            ...data,
            name: data.fileName,
            url: data.fileUri,
          };
          this.uploadDisabled = true;
          this.uploadFileList = [fileObj];
        } else {
          this.uploadDisabled = false;
          this.uploadFileList = [];
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      uploadFileList: [],
      uploadDisabled: false,
      loading: false,
    };
  },

  methods: {
    async httpRequest(data) {
      this.uploadDisabled = true;
      this.loading = true;
      const formData = new FormData();
      formData.append('upload', data.file);
      formData.append('businessTable', this.businessTable);
      formData.append('businessId', this.businessId);
      formData.append('subType', this.subType);
      try {
        const [res] = await publicApi.uploadFile(formData);
        this.$emit('input', res);
        this.$emit('change', res);
      } catch (error) {
        this.uploadDisabled = false;
      } finally {
        this.loading = false;
      }
    },
    handleRemove() {
      this.$emit('input', {});
      this.$emit('change', {});
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    containSpecial(str) {
      var containSpecial = RegExp(/[/\\<>{}~!@#$%^&*]+/);
      return containSpecial.test(str);
    },
    beforeUpload(file) {
      console.log(file);
      if (this.containSpecial(file.name)) {
        this.$message.error('文件名不能包含特殊字符');
        return false;
      }
      //23-06-26：水利部只接受jpg格式的图片！！
      if (
        file.type !== 'image/jpg' && file.type !== 'image/jpeg'
      ) {
        this.$message.error('上传图片只能是JPG格式!');
        return false;
      }
      if (this.minSize && file.size / 1024 < this.minSize) {
        this.$message.error(`上传图片大小不能小于 ${this.minSize}KB!`);
        return false;
      }
      if (this.maxSize && file.size / 1024 > this.maxSize) {
        this.$message.error(`上传图片大小不能超过 ${this.maxSize}KB!`);
        return false;
      }
      return true;
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep {
  .disabled .el-upload--picture-card {
    display: none !important;
  }
}
</style>
